var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    showSealDialog:false,
    sealList:[],
    selectSealIds:[],
    selectSealName:'请选择充值印章',
    rechargePackageList:[],
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '印章充值', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad() {
    requestClient = new ZYRequestClient();
  },
  onShow(){
    var userInfo = wx.getStorageSync('userInfo');
    if(userInfo==''){
      wx.showModal({
        title:'授权登录',
        content:'请授权登录使用更多功能！',
        showCancel:false,
        success(r){
          wx.navigateTo({
            url: '../../../login/index',
          })
        }
      })
    } else {
      this.loadRechargePackageList();
    }
  },
  // 加载公司印章列表
  loadCompanySealList(){
    var _this = this;
    var defaultCompany = wx.getStorageSync('defaultCompany');
    if(defaultCompany == ''){
      defaultCompany = { id:0 };
    }
    _this.setData({
      companyId:defaultCompany.id
    });
    requestClient.getSealList(defaultCompany.id,function(res){
      _this.setData({
        sealList:res.data.data
      });
    });
  },
  // 加载充值套餐列表
  loadRechargePackageList(){
    var _this = this;
    requestClient.getRechargePackageList(function(res){
      _this.setData({
        rechargePackageList:res.data.data
      });
    });
  },
  // 显示印章选择框
  bindShowSealDialog(e){
    var _this = this;
    _this.setData({
      showSealDialog:true
    });
    _this.loadCompanySealList();
  },
  // 确认选择
  bindSelectedSealClick(e){
    var _this = this;
    _this.setData({
      showSealDialog:false,
      selectSealName:'请选择充值印章'
    });
    // 拼接选中的印章
    var selectSealName = [];
    for(var i=0;i<_this.data.selectSealIds.length;i++){
      var seal = _this.data.sealList.find(seal=>seal.id == _this.data.selectSealIds[i]);
      var name = seal.name;
      selectSealName.push(name);
    }
    if(selectSealName.length > 0){
      _this.setData({
        selectSealName:selectSealName.join(',')
      });
    }
  },
  // 勾选印章
  checkboxChange(e){
    var _this = this;
    _this.setData({
      selectSealIds:[e.detail.value]
    });
  },
  // 去充值
  goRecharge(e){
    var _this = this;
    // 判断是否选择印章
    if(_this.data.selectSealIds.length == 0){
      wx.showToast({
        title: '请选择充值印章!',
        icon:'none'
      })
      return;
    }
    var userInfo = wx.getStorageSync('userInfo');
    var openId = userInfo.openId;
    if(openId){

    } else {
      wx.showModal({
        title:'登录超时',
        content:'登录超时，请重新登录',
        showCancel:false,
        success(r){
          wx.redirectTo({
            url: '../../../login/index',
          })
        }
      })
      return;
    }
    var packageId = e.currentTarget.dataset.id;
    // 构造充值数据
    var orderData = {
      openId:openId,
      companyId:_this.data.companyId,
      sealIdList:_this.data.selectSealIds,
      rechargePackageId:packageId
    };
    wx.showLoading({
      title: '充值中...',
      mask:true
    });
    // 创建订单
    requestClient.rechargeSeal(orderData,function(response){
      wx.hideLoading({
        success: (res) => {},
      })
      if(response.data.return_code == "SUCCESS" && response.data.result_code == "SUCCESS"){
        // 拉起支付
        wx.requestPayment({
          nonceStr: response.data.nonceStr,
          package: response.data.package,
          paySign: response.data.paySign,
          signType:response.data.signType,
          timeStamp: response.data.timeStamp,
          success(res){
            // 查询订单，同步服务费时间
            requestClient.queryOrder(response.data.out_trade_no,function(rsp){
              wx.showModal({
                title:'充值成功',
                content:'您是否需要查看充值记录？',
                confirmText:'去查看',
                cancelText:'以后再说',
                success(r){
                  if(r.confirm){
                    wx.navigateTo({
                      url: '../../mine/rechargerecord/index',
                    })
                  }
                }
              }) 
            })
          },
          fail(res){
            wx.showToast({
              title: '支付未完成！',
              icon: 'none',
              duration: 2000
            })
          }
        })
      } else {
        wx.showToast({
          title: '下单失败！',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})
